package com.seecen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seecen.mapper.CuisineCategoriesMapper;
import com.seecen.mapper.RecipeCategoryRelationMapper;
import com.seecen.pojo.CuisineCategories;
import com.seecen.pojo.RecipeCategoryRelation;
import com.seecen.pojo.Recipes;
import com.seecen.service.CuisineMainCategoriesService;
import com.seecen.service.RecipeCategoryRelationService;
import com.seecen.service.RecipesService;
import com.seecen.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CuisineMainCategoriesService mainCategoriesService;

    @Autowired
    private RecipeCategoryRelationService recipeCategoryRelationService;

    @Autowired
    private RecipesService recipesService;

    @Autowired
    private RecipeCategoryRelationMapper relationMapper;

    @Autowired
    private CuisineCategoriesMapper categoriesMapper;

    //获取所有大分类和相应小分类
    @GetMapping("getAllCategories")
    public Result getAllCategories() {
        Result ret = mainCategoriesService.findAllWithSubCategories();
        return ret;
    }

    //按小类别获取食谱
    @GetMapping("getRecipesByCategory")
    public Result getRecipesByCategory(@RequestParam Integer categoryId) {
        Result ret = recipeCategoryRelationService.findRecipesByCategoryId(categoryId);
        return ret;
    }

    @GetMapping("/search")
    public Result<Map<String, Object>> searchRecipes(@RequestParam String keyword) {
        // 1. 搜索菜谱
        List<Recipes> recipes = recipesService.searchRecipesByKeyword(keyword);

        if (recipes.isEmpty()) {
            return Result.ok(Collections.emptyMap());
        }

        // 2. 获取这些菜谱的所有分类ID
        List<Integer> recipeIds = recipes.stream()
                .map(Recipes::getRecipesId)
                .collect(Collectors.toList());

        List<RecipeCategoryRelation> relations = relationMapper.selectList(
                new QueryWrapper<RecipeCategoryRelation>().in("recipe_id", recipeIds)
        );

        // 3. 获取分类详情
        Set<Integer> categoryIds = relations.stream()
                .map(RecipeCategoryRelation::getCategoryId)
                .collect(Collectors.toSet());

        List<CuisineCategories> categories = Collections.emptyList();
        if (!categoryIds.isEmpty()) {
            categories = categoriesMapper.selectBatchIds(categoryIds);
        }

        // 4. 构建返回数据
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("recipes", recipes);
        resultMap.put("categories", categories.stream()
                .map(CuisineCategories::getCategoryName)
                .collect(Collectors.toList()));

        return Result.ok(resultMap);
    }
}
